using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using KSS.Generator;

namespace KSSGenerator
{
    public partial class DbmlToProjects : Form
    {
        KSS.Generator.MainGenerator mainGenerator = new MainGenerator();

        public DbmlToProjects()
        {
            InitializeComponent();
        }

        public string ConfigFilePath
        {
            get
            {
                return mainGenerator.ConfigurationFile;
            }
        }

        public void OpenConfigurationFile(string configurationFile)
        {
            try
            {
                mainGenerator.Open(configurationFile);
                ctrlGeneratorSettings.Setting = mainGenerator.Setting;
                txtDatabaseFile.Text = mainGenerator.Setting.DatabaseFilePath;
                txtTemplatePath.Text = mainGenerator.Setting.TemplateFolderPath;
                txtGeneratedPath.Text = mainGenerator.Setting.GeneratedFolderPath;
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void SaveSetting(string savedPath)
        {
            if (mainGenerator.Setting == null)
            {
                return;
            }

            ctrlGeneratorSettings.GetValueFromDesign();
            mainGenerator.SaveSetting(savedPath);
        }

        public void Generate()
        {
            mainGenerator.Setting.DatabaseFilePath = txtDatabaseFile.Text;
            mainGenerator.Setting.TemplateFolderPath = txtTemplatePath.Text;
            mainGenerator.Setting.GeneratedFolderPath = txtGeneratedPath.Text;

            try
            {
                mainGenerator.Generate();
                MessageBox.Show("Generate complete !!!");
            }
            catch (System.Exception e)
            {
                MessageBox.Show("Error: " + e.Message);
            }            
        }

        private void btnBrowseDatabaseFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtDatabaseFile.Text = dlg.FileName;
            }
        }

        private void btnBrowseTemplatePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtTemplatePath.Text = dlg.SelectedPath;
            }
        }

        private void btnBrowseGeneratedPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtGeneratedPath.Text = dlg.SelectedPath;
            }
        }
    }
}